﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Admin/Site.Master" AutoEventWireup="true" CodeBehind="AreaList.aspx.cs" Inherits="VK.Casemeet.Web.Admin.AreaList" %>
<asp:Content ID="Content1" ContentPlaceHolderID="contentHead" runat="server">
    <script type="text/javascript">
        window.onload = function () {
            
        }
        function clickTree(sender) {
            var imgclass = $(sender).attr("class");
            if (imgclass == "treeOpen hand") {
                TreeOpen(sender);
            } else {
                TreeClose(sender);
            }
        }

        function TreeClose(sender) {
            if ($(sender).attr("ischild") == "true" || $(sender).attr("ischild") == "True") {
                RemoveAreaTR($(sender).attr("id"));
                $(sender).attr("class", "treeOpen hand");
                $(sender).attr("style", "text-indent:" + GeLevelEmpty(Number($(sender).attr("level"))) + "px;background-position:" + GeLevelEmpty(Number($(sender).attr("level"))) + "px -210px;");
            }
        }

        function RemoveAreaTR(id) {
            $("#AreaTable tbody tr[parentID='" + id + "']").each(function (i, n) {
                RemoveAreaTR($(n).find("td:first").html());
                $(n).remove();
            });
        }

        function TreeOpen(sender) {
            $.ajax({
                type: "post",
                url: "/Admin/CommService.asmx/GetArea",
                dataType: "json",
                contentType: "application/json;charset=utf-8",
                data: "{ \"parentID\":" + $(sender).attr("id") + " }",
                beforeSend: function (jsonHttpRequest) {
                    this;
                },
                success: function (jsonHttpRequest, textStatus) {
                    var temp_option = new String;
                    $.each(jsonHttpRequest.d, function (i, field) {
                        temp_option += "<tr parentid='" + field.ParentID + "'>" +
                            "<td class='txtcen'>" + field.AreaID + "</td>" +
                            "<td style='text-indent:" + GeLevelEmpty(field.Level) + "px;background-position:" + GeLevelEmpty(field.Level) +
                            "px -" + (field.HasChild ? '210' : '240') + "px;' id='" + field.AreaID + "' class='" +
                            (field.HasChild ? 'treeOpen' : 'treeClose') + " hand' ischild='" + field.HasChild + "' level='" + 
                            field.Level + "' onclick='clickTree(this);'>&nbsp;" + field.Name + "</td>" +
                            "<td class='txtcen'>" + field.Level + "</td>" +
                            "<td class='txtcen'>" + field.Sort + "</td>" +
                            "<td>" +
                            "    <div class='left tableOperMoveUp' onclick='SetAreaMoveUp(this);'>&nbsp;</div>" +
                            "    <div class='left tableOperMoveDown' onclick='SetAreaMoveDown(this);'>&nbsp;</div>" +
                            "    <a class='left tableOperNew' href='AreaAdd.aspx?ParentID=" + field.AreaID + "'>&nbsp;</a>" +
                            "    <a class='left tableOperEdit' href='AreaEdit.aspx?AreaID=" + field.AreaID + "'>&nbsp;</a>" +
                            "    <div class='left tableOperDelete' onclick='DeleteArea(this);'>&nbsp;</div>" +
                            "</td>" +
                            "</tr>";
                    });
                    $(sender).parent().after(temp_option);
                    $(sender).attr("class", "treeClose hand");
                    $(sender).attr("style", "text-indent:" + GeLevelEmpty(Number($(sender).attr("level"))) + "px;background-position:" + GeLevelEmpty(Number($(sender).attr("level"))) + "px -240px;");
                },
                error: function (jsonHttpRequest, textStatus, errorThrown) {
                    this;
                },
                complete: function (jsonHttpRequest, textStatus) {
                    this;
                }
            });
        }

        function DeleteArea(sender) {
            if (confirm('您确定要删除吗？删除后将不可恢复！')) {
                $.ajax({
                    type: "post",
                    url: "/Admin/CommService.asmx/DeleteArea",
                    dataType: "json",
                    contentType: "application/json;charset=utf-8",
                    data: "{ \"areaID\":" + $(sender).parent().parent().find("td").eq(1).attr("id") + " }",
                    beforeSend: function (jsonHttpRequest) {
                        this;
                    },
                    success: function (jsonHttpRequest, textStatus) {
                        switch (jsonHttpRequest.d) {
                            case 1: 
                                var control = $("#AreaTable").find("td[id='" + $(sender).parent().parent().attr("parentid") + "']");
                                TreeClose(control);
                                TreeOpen(control);
                                break;
                            case 2: alert("分类存在子类不能被删除，请先删除所有子类。"); break;
                            case 3: alert("总分类不能删除!"); break;
                            case 4: alert("该分类不能被删除，请先删除或转移分类下所有信息。"); break;
                        }
                    },
                    error: function (jsonHttpRequest, textStatus, errorThrown) {
                        this;
                    },
                    complete: function (jsonHttpRequest, textStatus) {
                        this;
                    }
                });
            }
        }

        function SetAreaMoveUp(sender) {
            $.ajax({
                type: "post",
                url: "/Admin/CommService.asmx/SetAreaMoveUp",
                dataType: "json",
                contentType: "application/json;charset=utf-8",
                data: "{ \"areaID\":" + $(sender).parent().parent().find("td").eq(1).attr("id") + " }",
                beforeSend: function (jsonHttpRequest) {
                    this;
                },
                success: function (jsonHttpRequest, textStatus) {
                    if (jsonHttpRequest.d == 1) {
                        var control = $("#AreaTable").find("td[id='" + $(sender).parent().parent().attr("parentid") + "']");
                        TreeClose(control);
                        TreeOpen(control);
                    }
                },
                error: function (jsonHttpRequest, textStatus, errorThrown) {
                    this;
                },
                complete: function (jsonHttpRequest, textStatus) {
                    this;
                }
            });
        }

        function SetAreaMoveDown(sender) {
            $.ajax({
                type: "post",
                url: "/Admin/CommService.asmx/SetAreaMoveDown",
                dataType: "json",
                contentType: "application/json;charset=utf-8",
                data: "{ \"areaID\":" + $(sender).parent().parent().find("td").eq(1).attr("id") + " }",
                beforeSend: function (jsonHttpRequest) {
                    this;
                },
                success: function (jsonHttpRequest, textStatus) {
                    if (jsonHttpRequest.d == 1) {
                        var control = $("#AreaTable").find("td[id='" + $(sender).parent().parent().attr("parentid") + "']");
                        TreeClose(control);
                        TreeOpen(control);
                    }
                },
                error: function (jsonHttpRequest, textStatus, errorThrown) {
                    this;
                },
                complete: function (jsonHttpRequest, textStatus) {
                    this;
                }
            });
        }

        function GeLevelEmpty(level) {
            var withpx = 0;
            if (level > 1)//判断节点深度 大于1就是表示它是子节点
            {
                withpx = (level - 1) * 20; //注意这里是个全角空格
            }
            return withpx;
        }
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="contentBody" runat="server">
    <div class="bodyContent divBody">
        <div class="divListOper">
            <a class="tableOperNew" href="AreaAdd.aspx?ParentID=0">新建省级</a>
        </div>

        <asp:Repeater ID="rptArea" runat="server">
        <HeaderTemplate>
        <table id="AreaTable" class="tableList" cellspacing="0">
            <thead>
                <tr>
                    <th style="width:50px;">编号</th>
                    <th>名称</th>
                    <th style="width:50px;">层次</th>
                    <th style="width:50px;">排序</th>
                    <th style="width:150px;"><span>操作</span></th>
                </tr>
            </thead>
            <tbody>
        </HeaderTemplate>
        <ItemTemplate>
                <tr parentid='<%# Eval("ParentID")%>'>
                    <td class="txtcen"><%# Eval("AreaID")%></td>
                    <td style='text-indent:0px' id='<%# Eval("AreaID")%>' class='<%# bool.Parse(Eval("HasChild").ToString())?"treeOpen":"treeClose" %> hand' level='<%# Eval("Level")%>' ischild='<%# Eval("HasChild")%>' onclick="clickTree(this);">&nbsp;<%# Eval("Name") %></td>
                    <td class="txtcen"><%# Eval("Level")%></td>
                    <td class="txtcen"><%# Eval("Sort")%></td>
                    <td class="txtcen" style="width:140px;">
                        <div class='left tableOperMoveUp' onclick='SetAreaMoveUp(this);'>&nbsp;</div>
                        <div class='left tableOperMoveDown' onclick='SetAreaMoveDown(this);'>&nbsp;</div>
                        <a class="left tableOperNew" href="AreaAdd.aspx?ParentID=<%# Eval("AreaID")%>">&nbsp;</a>
                        <a class="left tableOperEdit" href="AreaEdit.aspx?AreaID=<%# Eval("AreaID")%>">&nbsp;</a>
                        <div class='left tableOperDelete' onclick='DeleteArea(this);'>&nbsp;</div>
                    </td>
                </tr>
        </ItemTemplate>
        <FooterTemplate>
            </tbody>
        </table>
        </FooterTemplate>
        </asp:Repeater>
    </div>
</asp:Content>
